<?php

/**
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: goods.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
/*判断页面是否在允许时间内*/
if ($_CFG['is_yugou']==0) {
 header('location:index.php');
}
assign_template();
$goods_id = !empty($_GET['id']) ? intval($_GET['id']) : '';
$area_id = !empty($_SESSION['area_id'])?intval($_SESSION['area_id']) : '1';
$act = !empty($_GET['act']) ? $_GET['act'] : '';
$smarty->assign('url', "pre_order_goods.php?id=$goods_id");
$_LANG['kilogram'] = '千克';
$_LANG['gram'] = '克';
$_LANG['home'] = '首页';
$_LANG['goods_attr'] = '';
$smarty->assign('goods_id', $goods_id);
$smarty->assign('top_title', $_LANG['goods_note']);


//获取商品信息

$goods_info = get_goods_info($goods_id);
//取得该商品预购价格
$goods_info['preferential_price'] = get_pre_order_price($goods_id);

/* 获得正在由你定价商品的信息 */
$by_your_pricing_goods = get_by_your_pricing_goods($goods_id);
$smarty->assign('by_your_pricing_goods',$by_your_pricing_goods);

if ($goods_info === false || !$goods_info['is_on_sale'])
{
   /* 如果没有找到任何记录则跳回到首页 */
   ecs_header("Location: ./\n");
   exit;
}
$is_buy=is_buy()?1:0;
$goods_info['goods_name'] = encode_output($goods_info['goods_name']);
$goods_info['goods_brief'] = encode_output($goods_info['goods_brief']);
$goods_info['promote_price'] = $goods_info['promote_price'];
if($goods_info['promote_end_date']<time()){
    $goods_info['promote_price']='';
}


$goods_info['market_price'] = $goods_info['market_price'];
//$goods_info['shop_price'] = encode_output($goods_info['goods_price']);
$goods_info['shop_price_formated'] = encode_output($goods_info['shop_price_formated']);
$goods_info['goods_number'] = encode_output($goods_info['goods_number']);
$goods_info['comment_rank'] = $goods_info['comment_rank']*20;
$smarty->assign('is_buy', $is_buy);


//商品促销中处理
if($goods_info['promote_end_date']>time() && $goods_info['promote_start_date']<time()){
    $goods_info['goods_number']=limit_num($goods_id);
    $smarty->assign('end_time',        ($goods_info['promote_end_date']-time())); 
    $smarty->assign('end_time_now',    promote_time_out($goods_info['promote_end_date']-time())); 
}


$smarty->assign('goods_info', $goods_info);
for ($i=1; $i <= $goods_info['goods_number']; $i++) { 
    $stock[$i]=$i;
}
$smarty->assign('stock', $stock);

$shop_price   = $goods_info['shop_price'];
$smarty->assign('footer', get_footer());
// 获取商品所有图片
$sql = 'SELECT img_id, img_desc, thumb_url, img_url'.
       ' FROM ' .$ecs->table('goods_gallery').
       " WHERE goods_id = '$goods_id' ORDER BY img_id";
$img_list = $db->getAll($sql);
    foreach ($img_list AS $key => $img)
    {
        $gallery["pic_".$key] = get_image_path($_REQUEST['id'], $img_list[$key]['img_url'], false, 'gallery');
    }
$smarty->assign('pic',  $gallery);
if(!empty($_REQUEST['act']) && $_REQUEST['act'] == 'click' && !empty($_REQUEST['id'])){
    $db->query('UPDATE ' . $ecs->table('goods') . " SET click_count = click_count + 1 WHERE goods_id = '$_REQUEST[id]'");
    $row = $db->getRow('SELECT click_count FROM ' . $ecs->table('goods') . " WHERE goods_id = '$_REQUEST[id]'");
    echo $row['click_count'];
    exit;
}


/* 检查是否有商品品牌 */
if (!empty($goods_info['brand_id']))
{
    $brand_name = $db->getOne("SELECT brand_name FROM " . $ecs->table('brand') . " WHERE brand_id={$goods_info['brand_id']}");
    $smarty->assign('brand_name', encode_output($brand_name));
}

/* 显示分类名称 */
$cat_array = get_parent_cats($goods_info['cat_id']);
krsort($cat_array);
$cat_str = '';
foreach ($cat_array as $key => $cat_data)
{
    $cat_array[$key]['cat_name'] = encode_output($cat_data['cat_name']);
    $cat_str .= "<a href='category.php?c_id={$cat_data['cat_id']}'>" . encode_output($cat_data['cat_name']) . "</a>-&gt;";
}
$smarty->assign('cat_array', $cat_array);
$properties = get_goods_properties($goods_id);  // 获得商品的规格和属性
$smarty->assign('specification',       $properties['spe']);  // 商品规格
foreach ($properties['spe'] as $key => $value) {
    if($value['name']=='颜色'){
        $color=$value['values'];
        $smarty->assign('color',       $color);  // 商品颜色
    }
}

$smarty->assign('cfg',          $_CFG);
$cmt = assign_comment($goods_id,0);
$smarty->assign('id',           $goods_id);
$smarty->assign('type',         0);
$smarty->assign('comments',     $cmt['comments']);
$smarty->assign('pager',        $cmt['pager']);
$smarty->assign('comment', $comment);//评论
$linked_goods = get_linked_goods($goods_id);// 关联商品
$smarty->assign('related_goods',       $linked_goods);  

$smarty->assign('daodansong_list',       get_daodansong());  


if(!is_cod()){
    $smarty->assign('is_shipping',       1); //不支持货到付款
}


// 最组合内容页处理
if ($goods_info['cat_id'] == 101) {
    /* 最组合商品信息处理 */
    if($goods_info['compose_note']){
        $properties = array();
        $compose_colors = array();
        foreach (explode(',',$goods_info['compose_note']) as $key => $value) {
            $compose_goods[$key]['compose_goods_sku'] = explode('X',current(explode('*',$value)));//将最组合商品SKU组合成数组
            $compose_goods[$key]['goods_number'] = end(explode('*',$value));            
            foreach ($compose_goods[$key]['compose_goods_sku'] as $k => $v) {
                //读取最组合颜色
                $sql = "SELECT ga.* FROM " . $ecs->table('goods_attr') . " ga LEFT JOIN " . $ecs->table('goods') . " g ON g.goods_id = ga.goods_id WHERE ga.attr_id = 1 AND g.goods_sku = $v";
                if($row = $db->getRow($sql)){
                   
                    $properties = get_goods_properties($row['goods_id']);//获取有颜色商品的颜色
                    $compose_colors = array_merge($compose_colors,$properties['spe'][1]['values']);//合并商品颜色
                    $compose_colors_number = $compose_goods[$key]['goods_number'];
                }
            }
        }

        $smarty->assign('compose_colors',          $compose_colors);                              // 最组合颜色
        $smarty->assign('compose_colors_number',   $compose_colors_number);                       // 最组合颜色自选数量
    }
    $smarty->display('goods_compose.dwt',      $cache_id);
}else{
    $smarty->display('pre_order_goods.dwt',      $cache_id);
}




/**
 * 获得指定商品的各会员等级对应的价格
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_user_rank_prices($goods_id, $shop_price)
{
    $sql = "SELECT rank_id, IFNULL(mp.user_price, r.discount * $shop_price / 100) AS price, r.rank_name, r.discount " .
            'FROM ' . $GLOBALS['ecs']->table('user_rank') . ' AS r ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('member_price') . " AS mp ".
                "ON mp.goods_id = '$goods_id' AND mp.user_rank = r.rank_id " .
            "WHERE r.show_price = 1 OR r.rank_id = '$_SESSION[user_rank]'";
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {

        $arr[$row['rank_id']] = array(
                        'rank_name' => htmlspecialchars($row['rank_name']),
                        'price'     => price_format($row['price']));
    }

    return $arr;
}
/**
 * 获得指定商品的关联商品
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_linked_goods($goods_id)
{
    $sql = 'SELECT g.goods_id,g.goods_name, g.goods_brief ' .
            'FROM ' . $GLOBALS['ecs']->table('link_goods') . ' lg ' .
            'LEFT JOIN ' . $GLOBALS['ecs']->table('goods') . ' AS g ON (g.goods_id = lg.link_goods_id OR g.goods_id =lg.goods_id)' .
            "WHERE lg.goods_id = '$goods_id' AND g.is_alone_sale = 1 AND g.is_delete = 0  AND g.is_on_sale = 1 ".
            "GROUP BY g.goods_id";

    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']     = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']   = $row['goods_name'];
        $arr[$row['goods_id']]['goods_brief']  = end(explode('-',$row['goods_name']));
        $arr[$row['goods_id']]['url']          = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
    }

    return $arr;
}

/**
 * 获得正在由你定价商品的商品信息
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  array
 */
function get_by_your_pricing_goods($goods_id)
{
     $sql = 'SELECT g.goods_id, g.goods_name, g.goods_thumb, g.shop_price, g.market_price ' .
            'FROM ' . $GLOBALS['ecs']->table('goods') . ' AS g ' . 
            "WHERE g.is_best = 1 AND g.is_new AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.is_on_sale = 1 ORDER BY g.sort_order,g.goods_id ASC";


    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$row['goods_id']]['goods_id']     = $row['goods_id'];
        $arr[$row['goods_id']]['goods_name']   = $row['goods_name'];
        $arr[$row['goods_id']]['url']          = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
        $arr[$row['goods_id']]['discount']     = intval(($row['shop_price'])*10/$row['market_price']);
    }
    return $arr;
}


/**
 * 获得预购信息
 *
 * @access  public
 * @param   integer     $goods_id
 * @return  float
 */

function get_pre_order_price($goods_id){
    $res = $GLOBALS['db']->getOne("SELECT `pre_order_price` FROM {$GLOBALS['ecs']->table('pre_order')} WHERE `goods_id`={$goods_id} ORDER BY end_time DESC LIMIT 1");
    return $res;
}

?>